package com.usabb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.AttachDO;
import com.usabb.service.AttachService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * <pre>
 * 附件表
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Slf4j
@Controller
@RequestMapping("/usabb/attach")
public class AttachController extends BaseController {
    @Autowired
    private AttachService attachService;

    @GetMapping()
    @RequiresPermissions("usabb:attach:attach")
    String Attach() {
        return "usabb/attach/attach";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("usabb:attach:attach")
    public Result<Page<AttachDO>> list(AttachDO attachDTO) {
        log.info("input attachDTO: {}", attachDTO);

        Wrapper<AttachDO> wrapper = new EntityWrapper<>(attachDTO).orderBy("id", false);
        Page<AttachDO> page = attachService.selectPage(getPage(AttachDO.class), wrapper);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("usabb:attach:add")
    String add() {
        return "usabb/attach/add";
    }

    //-----------------------------------
    @GetMapping("/{bizId}/add")
    @RequiresPermissions("usabb:attach:add")
    String addUponBaseId(@PathVariable("bizId") Long bizId, Model model) {
        model.addAttribute("bizId", bizId);
        return "usabb/attachCenter/add";
    }

    @Log("添加附件表")
    @ResponseBody
    @PostMapping("/upload")
    @RequiresPermissions("usabb:attach:add")
    public Result<String> upload(AttachDO attach, @RequestParam("file") MultipartFile file1) {

        attachService.upload(attach, file1);

        return Result.ok();
    }
    @Log("添加附件表")
    @ResponseBody
    @PostMapping("/upload2")
    @RequiresPermissions("usabb:attach:add")
    public Result<String> upload2(AttachDO attach, @RequestParam("file") MultipartFile file1) {

        attachService.upload2(attach, file1);

        return Result.ok();
    }
    @Log("添加附件表")
    @ResponseBody
    @PostMapping("/upload3")
    @RequiresPermissions("usabb:attach:add")
    public Result<String> upload3(AttachDO attach, @RequestParam("file") MultipartFile file1) {

        attachService.upload3(attach, file1);

        return Result.ok();
    }


    @GetMapping("/edit/{id}")
    @RequiresPermissions("usabb:attach:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        AttachDO attach = attachService.selectById(id);
        model.addAttribute("attach", attach);
        return "usabb/attach/edit";
    }

    @Log("添加附件表")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("usabb:attach:add")
    public Result<String> save(AttachDO attach) {
        attachService.insert(attach);
        return Result.ok();
    }




    @Log("修改附件表")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("usabb:attach:edit")
    public Result<String> update(AttachDO attach) {
        boolean update = attachService.updateById(attach);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除附件表")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("usabb:attach:remove")
    public Result<String> remove(Long id) {
        attachService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除附件表")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("usabb:attach:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        attachService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }


}
